<template>
  <!-- 用户未登录页面 -->
  <div class="my-container">
    <div class="header" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img src="@/assets/mobile.png" />
        <span>登录/注册</span>
      </div>
    </div>
    <!-- 用户已登录页面 -->
    <div class="header" v-else>
      <div class="user-data">
        <div class="user-data-left">
          <van-image fit="cover" round :src="usersList.photo" />
          <span v-text="usersList.name"></span>
        </div>
        <div class="user-data-right">
          <van-button type="default" size="mini" round to="/user/profile"
            >编辑用户</van-button
          >
        </div>
      </div>
      <div class="user-list">
        <div class="user-list-item">
          <span class="top-span">{{ usersList.art_count }}</span>
          <span class="bottom-span">头条</span>
        </div>
        <div class="user-list-item">
          <span class="top-span">{{ usersList.follow_count }}</span>
          <span class="bottom-span">关注</span>
        </div>
        <div class="user-list-item">
          <span class="top-span">{{ usersList.fans_count }}</span>
          <span class="bottom-span">粉丝</span>
        </div>
        <div class="user-list-item">
          <span class="top-span">{{ usersList.like_count }}</span>
          <span class="bottom-span">获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2" class="nav-grid" clickable>
      <van-grid-item class="nav-grid-left" @click="getFollows" >
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="nav-grid-right">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      title="退出登录"
      class="logout-cell"
      v-if="user"
      @click="offLogin"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUser, getFollow } from '@/api/users'
export default {
  name: 'MyIndex',
  data () {
    return {
      usersList: {}
    }
  },
  created () {
    if (this.user) { this.loadUser() }
  },
  methods: {
    // 关闭按钮，清除本地token
    offLogin () {
      this.$dialog.confirm({
        message: '是否确认退出登录？'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUser () {
      try {
        const { data } = await getUser()
        console.log(data)
        this.usersList = data.data
      } catch (err) {
        this.$toast('获取数据失败')
        console.log(err)
      }
    },
    async getFollows () {
      try {
        const { data } = await getFollow({
          page: 1,
          per_page: 10
        })
        console.log(data, '收藏')
      } catch (err) {

      }
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less">
.my-container {
  .header {
    height: 361px;
    background: url(~@/assets/banner.png);
    background-size: cover;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100%;
      img {
        width: 132px;
        height: 132px;
      }
      span {
        margin-top: 15px;
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .header {
    .user-data {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      .user-data-left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        img {
          width: 132px;
          height: 132px;
          border: 5px solid #fff;
          margin-right: 23px;
        }
        span {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .user-list {
      display: flex;
      height: 130px;
      .user-list-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        flex: 1;
        color: #fff;
        .top-span {
          font-size: 36px;
        }
        .bottom-span {
          font-size: 24px;
        }
      }
    }
  }
  .nav-grid {
    margin-bottom: 9px;
    .van-grid-item {
      height: 141px;
      i {
        font-size: 45px;
      }
      span {
        font-size: 18px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
    }
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
    margin-top: 9px;
  }
}
</style>
